package backend.project.gym_management.controller;

import backend.project.gym_management.base.response.JsonResult;
import backend.project.gym_management.pojo.entity.User;
import backend.project.gym_management.pojo.dto.LoginParam;
import backend.project.gym_management.pojo.dto.UserRegisterParam;
import backend.project.gym_management.pojo.entity.Employee;
import backend.project.gym_management.service.AuthService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * AuthController类负责处理用户和员工的认证操作，如登录和注册
 */
@Slf4j
@Api(tags = "01.登录与注册模块")
@RestController
@RequestMapping("/auth")
public class AuthController {

    @Autowired
    private AuthService authService;

    /**
     * 用户登录接口
     *
     * @param param 包含用户账户和密码的登录参数
     * @param session 用于存储用户会话信息
     * @return 登录结果，包括成功消息
     */
    @PostMapping("/user/login")
    @ApiOperation("用户登录功能")
    @ApiOperationSupport(order = 10)
    public JsonResult userLogin(@RequestBody LoginParam param, @ApiIgnore HttpSession session) {
        log.debug(param.toString());
        User user = authService.userLogin(param.getAccount(), param.getPassword());
        List<String> data = new ArrayList<>();
        data.add("登录成功");
        data.add(String.valueOf(user.getUid()));
        data.add(String.valueOf(user.getTitle()));
        data.add(String.valueOf(user.getUsername()));
        return JsonResult.ok(data); // 添加成功提示
    }

    /**
     * 员工登录接口
     *
     * @param param 包含员工账户和密码的登录参数
     * @param session 用于存储员工会话信息
     * @return 登录结果，包括成功消息
     */
    @PostMapping("/employee/login")
    @ApiOperation("员工登录功能")
    @ApiOperationSupport(order = 20)
    public JsonResult employeeLogin(@RequestBody LoginParam param, @ApiIgnore HttpSession session) {
        Employee employee = authService.employeeLogin(param.getAccount(), param.getPassword());
        session.setAttribute("role", employee.getTitle().name()); // 设置角色为员工的职位名称
        session.setAttribute("eid", employee.getEid());
        List<String> data = new ArrayList<>();
        data.add("登录成功");
        data.add(employee.getTitle().name());
        data.add(String.valueOf(employee.getEid()));
        data.add(String.valueOf(employee.getTitle()));
        data.add(String.valueOf(employee.getUsername()));
        return JsonResult.ok(data); // 添加成功提示
    }

    /**
     * 用户注册接口
     *
     * @param param 包含用户注册信息的参数，包括账户、密码和用户名
     * @return 注册结果，包括成功消息
     */
    @PostMapping("/user/register")
    @ApiOperation("用户注册功能")
    @ApiOperationSupport(order = 30)
    public JsonResult register(@Valid @RequestBody UserRegisterParam param) {
        User user = new User();
        BeanUtils.copyProperties(param, user);
        user.setTitle(User.Title.正式会员); // 默认会员类型
        authService.register(user);
        return JsonResult.ok("注册成功");
    }
}
